package com.example.tanwei.controller;

import com.example.tanwei.pojo.User;
import com.example.tanwei.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * @Author: huang
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public List<User> list(){
        List<User> users = userService.getUsers();
        return users;
    }

    @GetMapping("/{id}")
    @ResponseBody
    public User getUserById(@PathVariable("id")int id){
        User user = userService.getUserById(id);
        return user;
    }

    @PostMapping("")
    @ResponseBody
    public String addUser(User user){
        System.out.println(user);
        user.setCreate_time(new Date());
        int flag = userService.addUser(user);
        if (flag==1){
            return "添加成功！";
        }else {
            return "添加失败！";
        }
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public String addUser(@PathVariable("id")int id){
        int flag = userService.deleteUserById(id);
        if (flag==1){
            return "删除成功！";
        }else {
            return "删除失败！";
        }
    }
}
